#include "replacerulesmodel.h"

ReplaceRulesModel::ReplaceRulesModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}

void ReplaceRulesModel::setList(QList< QPair<QRegExp, QString> > *replaceRulesList){
    m_replaceRulesList = replaceRulesList;
}

int ReplaceRulesModel::rowCount(const QModelIndex &/*parent*/) const {
    return m_replaceRulesList->count();
}

int ReplaceRulesModel::columnCount(const QModelIndex &/*parent*/) const {
    return 3;
}

QVariant ReplaceRulesModel::data(const QModelIndex &index, int role) const {
    if(!m_replaceRulesList)
        return QVariant();
    const QList< QPair<QRegExp, QString> > & list = *m_replaceRulesList;
    if(!index.isValid())
        return QVariant();
    if(role == Qt::DisplayRole){
        int r = index.row();
        int c = index.column();
        if(c==0){
            if(list[r].first.patternSyntax()==QRegExp::FixedString)
                return QString("FixedString");
            else if(list[r].first.patternSyntax()==QRegExp::Wildcard)
                return QString("Wildcard");
            else if(list[r].first.patternSyntax()==QRegExp::WildcardUnix)
                return QString("WildcardUnix");
            else if(list[r].first.patternSyntax()==QRegExp::RegExp)
                return QString("RegExp");
            else if(list[r].first.patternSyntax()==QRegExp::RegExp2)
                return QString("RegExp2");
        }
        else if(c==1){
            return list[r].first.pattern();
        }
        else if(c==2){
            return list[r].second;
        }
        else{
            return QVariant();
        }
    }
    return QVariant();
}

bool ReplaceRulesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
    if(!m_replaceRulesList)
        return false;
    QList< QPair<QRegExp, QString> > & list = *m_replaceRulesList;
    if(index.isValid() && (role==Qt::EditRole)){
        int r = index.row();
        int c = index.column();
        QString str = value.toString();
        switch(c){
        case 0:
            if(str=="FixedString") list[r].first.setPatternSyntax(QRegExp::FixedString);
            else if(str=="Wildcard") list[r].first.setPatternSyntax(QRegExp::Wildcard);
            else if(str=="WildcardUnix") list[r].first.setPatternSyntax(QRegExp::WildcardUnix);
            else if(str=="RegExp") list[r].first.setPatternSyntax(QRegExp::RegExp);
            else if(str=="RegExp2") list[r].first.setPatternSyntax(QRegExp::RegExp2);
            return true;
            break;
        case 1:
           list[r].first.setPattern(str);
           return true;
           break;
        case 2:
           list[r].second = str;
           return true;
           break;
        default:return false;
           break;
        }
    }
    return false;
}

QVariant ReplaceRulesModel::headerData(int section,Qt::Orientation orientation, int role) const {
    if(role== Qt::DisplayRole){
        if(orientation == Qt::Horizontal){
            switch(section){
                case 0:return tr("Syntax");
                       break;
                case 1:return tr("Pattern");
                       break;
                case 2:return tr("Replacement");
                       break;
                default:return QVariant();
                       break;
            }
        }
        else {
            return section;
        }
    }
    return QVariant();
}

Qt::ItemFlags ReplaceRulesModel::flags(const QModelIndex &index) const {
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    return flags|Qt::ItemIsEditable;
}

void ReplaceRulesModel::update(){
    reset();
}

bool ReplaceRulesModel::insertRows(int row, int count, const QModelIndex& parent){
    beginInsertRows(parent,row,row+count-1);
    QPair<QRegExp, QString> pair;
    for(int i = 0; i < count; ++i){
        m_replaceRulesList->insert(row+i,pair);
    }
    endInsertRows();
    return true;
}

bool ReplaceRulesModel::removeRows(int row, int count, const QModelIndex& parent){
    if(row+count-1>m_replaceRulesList->count())
        return false;
    beginRemoveRows(parent,row,row+count-1);
    for(int i = 0; i < count; ++i){
        m_replaceRulesList->removeAt(row);
    }
    endRemoveRows();
    return true;
}

bool ReplaceRulesModel::moveUp(int row, const QModelIndex& parent){
    if(row==0)
        return false;
    QPair<QRegExp, QString> data = m_replaceRulesList->at(row);
    beginRemoveRows(parent,row,row);
    m_replaceRulesList->removeAt(row);
    endRemoveRows();
    beginInsertRows(parent,row-1,row-1);
    m_replaceRulesList->insert(row-1,data);
    endInsertRows();
    return true;
}

bool ReplaceRulesModel::moveDown(int row, const QModelIndex& parent){
    if(row>=m_replaceRulesList->count()-1)
        return false;
    QPair<QRegExp, QString> data = m_replaceRulesList->at(row);
    beginRemoveRows(parent,row,row);
    m_replaceRulesList->removeAt(row);
    endRemoveRows();
    beginInsertRows(parent,row+1,row+1);
    m_replaceRulesList->insert(row+1,data);
    endInsertRows();
    return true;
}
